from django.shortcuts import render
from .models import UserInfo, BlogBody

from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

from random import randint

# from markdown import markdown


# 处理首页已经日常的视图,包含首页,列表显示,文章详情,以及各个type页的列表
def index(request):
    userinfo = UserInfo.objects.first()
    blog_body = BlogBody.objects.all().order_by('-id')[:5]
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    daoyu = '经过几个月的修修补补，网站还是做出来了，后台用的Django自己写的，前端页面是从GitHub上拖来后改的，\
            最难的还是从css里改样式，完全不懂该怎么改，全靠百度。现在代码还是比较乱，接下的工作来就要重构代码了， \
            不管则么说，自己能够做出一个完整的作品还是很开心的>_<'
    img_number = randint(1, 5)
    img_url = '/static/images/'+str(img_number)+'.jpg'
    return render(request, 'index.html', locals())


def lists(request):
    return render(request, 'python_list.html')


def article(request, blog_body_id=''):
    """
    处理点击事件,并且点击数加一
    """
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    blog_content = BlogBody.objects.get(id=blog_body_id)
    num = blog_content.blog_clicknum
    num += 1
    blog_content.blog_clicknum = num
    blog_content.save()
    return render(request, 'view.html', locals())


def python(request):
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    python = BlogBody.objects.filter(blog_type='Python')[::-1]
    python_blog = Getpag(request, python)
    return render(request, 'python_list.html', locals())


def abouttest(request):
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    test = BlogBody.objects.filter(blog_type='abouttest')[::-1]
    test_blog = Getpag(request, test)
    return render(request, 'test_list.html', locals())


def mytalk(request):
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    mytalk = BlogBody.objects.filter(blog_type='mytalk')[::-1]
    mytalk_blog = Getpag(request, mytalk)
    return render(request, 'mytalk_list.html', locals())


def diary(request):
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    diary = BlogBody.objects.filter(blog_type='diary')[::-1]
    diary_blog = Getpag(request, diary)
    return render(request, 'diary_list.html', locals())


# 分页函数
def Getpag(request, list):
    paginator = Paginator(list, 5)
    page = request.GET.get('page')
    try:
        informations = paginator.page(page)
    except PageNotAnInteger:
        informations = paginator.page(1)
    except EmptyPage:
        informations = paginator.page(paginator.num_pages)
    return informations


# 搜索函数
def search_html(request):
    keywords = request.POST['keywords']
    informations = BlogBody.objects.all()
    search_result = []
    for x in informations:
        if keywords in x.blog_title:
            search_result.append(x)
    search_status = 'Error' if len(search_result) == 0 else 'Success'
    result_amout = len(search_result)
    hot_rank = BlogBody.objects.all().order_by('-blog_clicknum')[:4]
    like_rank = BlogBody.objects.filter(blog_like=True)
    return render(request, "search.html", locals())

